package com.fengxu.netty.codec2;

import com.fengxu.netty.codec.StudentPOJO;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

import java.util.Random;

// 服务器管道处理器
public class NettyServerHandler extends ChannelInboundHandlerAdapter {

    // 读取通道中的数据
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        // 读取客户端发送的StudentPOJO.student对象
        MyDataInfo.MyMessage myMessage = (MyDataInfo.MyMessage) msg;
        MyDataInfo.MyMessage.DataType dataType = myMessage.getDataType();
        if(dataType == MyDataInfo.MyMessage.DataType.StudentType){
            System.out.println(myMessage.getStudent());
        } else if(dataType == MyDataInfo.MyMessage.DataType.WorkerType){
            System.out.println(myMessage.getWorker());
        }

    }

    // 异常处理,一般是关闭通道
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}
